"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.assembleFilename = exports.saveChatMessage = void 0;
const ClassroomEntity_1 = require("../httpServer/entity/ClassroomEntity");
const ChatEntity_1 = require("../httpServer/entity/ChatEntity");
const enums_1 = require("../utils/enums");
const fileTypeEnum = {
    audio: 'mp3',
    video: 'mp4'
};
async function saveChatMessage(data, classId, cb) {
    const classroom = await ClassroomEntity_1.ClassroomEntity.findOneBy({ classId, chatMode: enums_1.ChatModeEnums.CHAT });
    if (classroom) {
        let dbChat = new ChatEntity_1.ChatEntity();
        dbChat.userId = data.userId;
        dbChat.username = data.username;
        dbChat.roomId = classroom.id;
        dbChat.contentType = data.contentType;
        dbChat.content = data.content;
        dbChat.sendTime = Date.now().toString();
        dbChat = await dbChat.save();
        cb && cb(null, dbChat);
    }
    else {
        cb && cb({
            status: 'error',
            message: '发送失败！'
        });
    }
}
exports.saveChatMessage = saveChatMessage;
function assembleFilename(filename, fileType) {
    if (filename) {
        if (filename.indexOf('.') > -1) {
            filename = `${Date.now()}-${filename}`;
        }
        else {
            filename = `${Date.now()}-${filename}.${fileTypeEnum[fileType]}`;
        }
    }
    else {
        filename = `${Date.now()}.${fileTypeEnum[fileType]}`;
    }
    return filename;
}
exports.assembleFilename = assembleFilename;
